package com.example.algorithm.string;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/7/25
 * 颠倒字符串
 */
public class InvertString {
    public String reverseWords(String s) {
        // 除去开头和末尾的空白字符
        s = s.trim();
        // 正则匹配连续的空白字符作为分隔符分割
        List<String> wordList = Arrays.asList(s.split("\\s+"));
        Collections.reverse(wordList);
        return String.join(" ", wordList);
    }
}
